using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;

namespace Xms.Abp.Emailing;

/// <summary>
/// This class is an implementation of <see cref="IEmailSender"/> as similar to null pattern.
/// It does not send emails but logs them.
/// </summary>
public class NullEmailSender : EmailSenderBase
{
    protected ILogger<NullEmailSender> Logger { get; set; }

    /// <summary>
    /// Creates a new <see cref="NullEmailSender"/> object.
    /// </summary>
    public NullEmailSender(
        IBackgroundJobManager backgroundJobManager)
        : base(backgroundJobManager)
    {
        Logger = NullLogger<NullEmailSender>.Instance;
    }

    protected override Task SendEmailAsync(EmailMessage emailMessage)
    {
        Logger.LogWarning("Email Sending was not implemented! Using NullEmailSender!");
        LogEmail(emailMessage);
        return Task.CompletedTask;
    }

    private void LogEmail(EmailMessage emailMessage)
    {
        Logger.LogWarning("TenantId    : " + emailMessage.TenantId);
        Logger.LogWarning("From        : " + emailMessage.From);
        Logger.LogWarning("Tos         : " + string.Join(",", emailMessage.Tos));
        Logger.LogWarning("CCs         : " + string.Join(",", emailMessage.CCs));
        Logger.LogWarning("Subject     : " + emailMessage.Subject);
        Logger.LogWarning("IsBodyHtml  : " + emailMessage.IsBodyHtml);
        Logger.LogWarning("Body        : ");
        Logger.LogWarning(emailMessage.Body);
    }
}
